﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Linq;
using ROOTFileSizeAnalyzer.Parsers;

namespace ROOTFileSizeAnalyzer
{
    /// <summary>
    /// Helps with parsing the output of a ROOT size guy.
    /// </summary>
    public class ROOTSizeParser
    {

        /// <summary>
        /// Parse input given by a string
        /// </summary>
        /// <param name="rootInputString"></param>
        /// <returns></returns>
        public IObservable<ROOTBranchInfo> ParseInput(string rootInputString)
        {
            ///
            /// If we are requested, get the data over the internet.
            /// 

            IObservable<string> lines = null;
            if (IsURLReference(rootInputString))
            {
                Uri u = new Uri(rootInputString);
                lines = u.ToLineObservable();
            }
            else
            {
                lines = rootInputString.ToLineObservable();
            }

            ///
            /// Load up a stream for reading...
            /// 

            var ltob = new LineToBranchConverter();
            lines.Subscribe(ltob);
            return ltob;
        }

        /// <summary>
        /// Return true if the string to be parsed will cause a URL reference
        /// </summary>
        /// <param name="rootInputString"></param>
        /// <returns></returns>
        public bool IsURLReference(string rootInputString)
        {
            return rootInputString.StartsWith("http://");
        }
    }
}
